package mo.tags.stack;

import java.util.Stack;

public class L1190 {

    public String reverseParentheses(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c != ')') {
                stack.push(c);
            } else {
                StringBuilder builder = new StringBuilder();
                while (stack.peek() != '(') {
                    builder.append(stack.pop());
                }
                stack.pop();
                for (int i = 0; i < builder.length(); i++) {
                    stack.push(builder.charAt(i));
                }
            }
        }
        StringBuilder res = new StringBuilder();
        while (!stack.isEmpty()) {
            res.append(stack.pop());
        }
        return res.reverse().toString();
    }


    public static void main(String[] args) {
        L1190 test = new L1190();
        System.out.println(test.reverseParentheses("a(bcdefghijkl(mno)p)q"));
    }
}
